﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;

namespace ServeNHL.Models
{
    [MetadataType(typeof(UserEventPrefMetadata))]
    public partial class USER_EVENT_PREF
    {
        public int saveEventPref(UserEventViewModel evtpref)
        {
            int eventPrefID = 0;

            if(evtpref.userEventPref.PREF_ID != 0)
            {
                updateEvtPref(evtpref.userEventPref);
            }

            else
            {
                insertEvtPref(evtpref.userEventPref);
            }

            eventPrefID = evtpref.userEventPref.PREF_ID;

            return eventPrefID;
        }//end of public int saveEventPref(eventViewModel evt)


        public int insertEvtPref(USER_EVENT_PREF evtPref)
        {
            int prefID = 0;
            
                using (var dbconn = new fclick_dbEntities())
            {
                var pref = new USER_EVENT_PREF {
                    EVENT_CAT_ID = evtPref.EVENT_CAT_ID,
                    USER_ID = evtPref.USER_ID,
                    INTEREST_LEVEL = evtPref.INTEREST_LEVEL,
                    STATUS = evtPref.STATUS,
                    CREATED_DATE = Helpers.createDate.getCreateDate(),
                    LAST_MOD_DATE = Helpers.createDate.getCreateDate()
                };

                dbconn.USER_EVENT_PREF.Add(pref);

                dbconn.SaveChanges();

                prefID = pref.PREF_ID;

            }//end of using (var dbconn = new fclick_dbEntities())
            return prefID;
        }// end of public int insertEvtPref(USER_EVENT_PREF evtPref, USER user, EVENT_CAT evtCat)


        public int updateEvtPref(USER_EVENT_PREF evtPref)
        {
            int prefId = 0;

            using (var dbconn = new fclick_dbEntities())
            {
                var pref = new USER_EVENT_PREF();

                if (evtPref.EVENT_CAT_ID > 0 || !String.IsNullOrEmpty(evtPref.EVENT_CAT_ID.ToString())) {
                    pref.EVENT_CAT_ID = evtPref.EVENT_CAT_ID;
                }
                if (evtPref.USER_ID > 0 || !String.IsNullOrEmpty(evtPref.USER_ID.ToString())){
                    pref.USER_ID = evtPref.USER_ID;
                }
                if (!String.IsNullOrEmpty(evtPref.INTEREST_LEVEL.ToString())){
                    pref.INTEREST_LEVEL = evtPref.INTEREST_LEVEL;
                }
                if (!String.IsNullOrEmpty(evtPref.STATUS)){
                    pref.STATUS = evtPref.STATUS;
                }

                dbconn.USER_EVENT_PREF.Add(pref);
                dbconn.SaveChanges();

                prefId = pref.PREF_ID;

            }// end of using (var dbconn =new fclick_dbEntities())
            return prefId;

        }//end of public int updateEvtPref(USER_EVENT_PREF evtPref, USER user, EVENT_CAT evtCat)
    }
}